/*
 * LoginAction.java
 *
 * Created on August 29, 2007, 11:01 PM
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package tcc.gui.web.struts.login;

import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;
import org.apache.struts.action.ActionMessages;
import tcc.applicationService.login.LoginAS;
import tcc.domain.login.TCCSystemUser;

/**
 *
 * @author Diego
 */
public class LoginAction extends Action{
    private static final String SUCCESS_FORWARD = "success";
    private static final String UNSUCCESS_FORWARD = "unsuccess";
    
    /** Creates a new instance of LoginAction */
    public LoginAction() {
    }
    
    @Override
    public ActionForward execute(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response) throws Exception {
            LoginForm loginForm = (LoginForm) form;
        TCCSystemUser userValidado = LoginAS.getInstance().canLogin(loginForm.getSystemUser());
        if (userValidado!=null){
            HttpSession session = request.getSession(true);
            session.setAttribute("user",userValidado);
            return mapping.findForward(SUCCESS_FORWARD);
        }else{
            ActionErrors errors = new ActionErrors();
            errors.add(ActionErrors.GLOBAL_MESSAGE,new ActionMessage("Login.jsp.loginInvalid"));
            if(!errors.isEmpty()){
                request.setAttribute("message", errors);
            }
            return mapping.findForward(UNSUCCESS_FORWARD);
            
        }
    }
}


